#!/usr/bin/python
from matplotlib.pylab import *
import sys
import os.path

def averages(filename):
	xt=[]; xm=[];
	n11m=[]; n22m=[]; n12m=[];   #Defines lists with no length
	n11d=[]; n22d=[]; n12d=[];
	d=load(filename)
	n1=d[:,0]; n2=d[:,1]   #all values in a column are assigned to a list
	n11=d[:,4]; n22=d[:,5]; n12=d[:,6]
	x=n1/(n1+n2)  #equation for mole fraction
	
	x0=x[0]   #first mole fraction=x0
	xt=[]; n11t=[]; n22t=[]; n12t=[];
	xt.append(x[0])
	n11t.append(n11[0])
	n22t.append(n22[0])   #adds n22 value for first row into the n22t (temp) list
	n12t.append(n12[0])
	
	#print x[0],n11[0]
	for i in range(1,len(x)):   #for all values in the range from 1 to the end of the list
		if x[i]==x0: # if the next value of x is equal to the first value
			#print x[i],n11[i]
			xt.append(x[i])
			n11t.append(n11[i])    # different values of same row are appended to their temp list
			n22t.append(n22[i])
			n12t.append(n12[i])
		else:   
			xm.append(mean(xt))
			n11m.append(mean(n11t))
			n22m.append(mean(n22t))    # if x is different, the averages and standard deviation are appended
			n12m.append(mean(n12t))    # to a permanent list
			n11d.append(std(n11t))
			n22d.append(std(n22t))
			n12d.append(std(n12t))
			
			x0=x[i]		# x0 is assigned a new value
			xt=[]; n11t=[]; n22t=[]; n12t=[];  #the lists are cleared
			#print x[i],n11[i]		
			xt.append(x[i])		# the next values are appended to the temp lists
			n11t.append(n11[i])
			n22t.append(n22[i])
			n12t.append(n12[i])	
	
	xm.append(mean(xt))
	n11m.append(mean(n11t))		#after the loop is completed, the remaining values in the temp files are averaged
	n22m.append(mean(n22t))		#and their standard deviations are found and appended to the permanent lists
	n12m.append(mean(n12t))
	n11d.append(std(n11t))
	n22d.append(std(n22t))
	n12d.append(std(n12t))
	out=[xm,n11m,n22m,n12m,n11d,n22d,n12d]   #the values are sorted into a list of the averages and standard deviations and returned
	return out


def graph(system,level="4"):		#arguments are the system name and level (default level=4)
	
	
	experiment=system+"-exp.txt"	#the files holding the data are assigned to variables and opened as exp
	ff1=system+"-sff.txt"
	ff2=system+"-skb.txt"
	
	while not os.path.isfile(experiment):  #determines if file exists
		strout="File "+experiment+" does not exist! Enter the whole file name: "
		experiment=raw_input(strout)
		
	while not os.path.isfile(ff1):  #determines if file exists
		strout="File "+ff1+" does not exist! Enter the whole file name: "	#error trapping just in case the file does not exist
		ff1=raw_input(strout)
		
	while not os.path.isfile(ff2):  #determines if file exists
		strout="File "+ff2+" does not exist! Enter the whole file name: "
		ff2=raw_input(strout)
		
	exp=load(experiment)
	plot(exp[:,0],exp[:,1],label='N11-exp',color='black')	#experimental data is plotted
	hold(True)	#holds the graph so it is not overwritten
	title(system)	#the title of the graph is the name of the system
	plot(exp[:,0],exp[:,2],label='N22-exp',color='red')
	plot(exp[:,0],exp[:,3],label='N12-exp',color='blue')
	
	if level=="1" or level=="4":
		out=averages(ff1)	#averages function is run for the first file and data plotted with error bars
		errorbar(out[0],out[1],out[4],label='N11-Siepmann',fmt='o',markerfacecolor='black',markeredgecolor='black',ecolor='black',linestyle='None')
		errorbar(out[0],out[2],out[5],label='N22-Siepmann',fmt='o',markerfacecolor='red',markeredgecolor='red',ecolor='red',linestyle='None')
		errorbar(out[0],out[3],out[6],label='N12-Siepmann',fmt='o',markerfacecolor='blue',markeredgecolor='blue',ecolor='blue',linestyle='None')
		
	if level=="3" or level=="4":
		out=averages(ff2)	#averages funcion is run for the second file and data plotted with errorbars
		errorbar(out[0],out[1],out[4],label='N11-KB',fmt='o',markerfacecolor='white',markeredgecolor='black',ecolor='black',linestyle='None')
		errorbar(out[0],out[2],out[5],label='N22-KB',fmt='o',markerfacecolor='white',markeredgecolor='red',ecolor='red',linestyle='None')
		errorbar(out[0],out[3],out[6],label='N12-KB',fmt='o',markerfacecolor='white',markeredgecolor='blue',ecolor='blue',linestyle='None')
		
	legend(loc=2)	#legend is located top left

nargs=len(sys.argv)-1

if nargs==0:
	system1=raw_input("What is the first system name? ")
	level1=raw_input("What is its level? ")
	system2=raw_input("What is the second system name? ")
	level2=raw_input("What is its level? ")
	
	
if nargs==1:
	system1=sys.argv[1]
	level1="4"
	
if nargs==2:
	system1=sys.argv[1]
	system2=sys.argv[2]
	level1="4"
	level2="4"

figure(figsize=(8,7))
graph(system1, level1)	#runs graph function of system1 at level1
savefig(system1+".png")	#saves a copy of the figure to the open file

if nargs==2 or nargs==0:
	figure(figsize=(7,6))	#creates another window for the following graph and sets the size of the window
	graph(system2, level2)		#graphs system2 at level2
	savefig(system2+".png")	#saves a copy of the figure to the open file

show()			#shows the figures
